package com.sux.demo;

public class WebSocketServerHost {
    public static void main(String[] args) {
        WebSocketServer webSocketServer = new WebSocketServer();

        SendDataToClientThread thread = new SendDataToClientThread(webSocketServer);
        thread.start();

        webSocketServer.start(40005, "WebSocket服务端");
    }
}

class SendDataToClientThread extends Thread {
    private WebSocketServer webSocketServer;

    private int index = 1;

    public SendDataToClientThread(WebSocketServer webSocketServer) {
        this.webSocketServer = webSocketServer;
    }

    @Override
    public void run() {
        try {
            while (index <= 5000000) {
                if (webSocketServer.hasClient()) {
                    String msg = "服务端发送的测试消息, index = " + index;
                    webSocketServer.send(msg);
                    index++;
                }
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
